package com.lenovo.manufacture.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.bean.JianKongBean;
import com.lenovo.manufacture.utils.Okhttp;
import com.lenovo.manufacture.view.MyPopupWindow;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class JianKongActivity extends AppCompatActivity implements View.OnClickListener {


    private Button ivBack;
    private TextView tvTitle;
    private List<ImageButton> buttons = new ArrayList<>();
    private MyPopupWindow myPopupWindow;
    private String url = "http://297v1s5722.zicp.vip/dataInterface/PLStep/getAll";
    private List<JianKongBean.DataBean> data;
    private String json = "{\"status\":200,\"message\":\"SUCCESS\",\"data\":[{\"id\":5,\"plStepName\":\"MPV车型生产环节1\",\"step\":1,\"power\":100,\"consume\":30,\"icon\":\"line02_01\"},{\"id\":6,\"plStepName\":\"MPV车型生产环节2\",\"step\":2,\"power\":100,\"consume\":30,\"icon\":\"line02_02\"},{\"id\":7,\"plStepName\":\"MPV车型生产环节3\",\"step\":3,\"power\":100,\"consume\":30,\"icon\":\"line02_03\"},{\"id\":8,\"plStepName\":\"MPV车型生产环节4\",\"step\":4,\"power\":100,\"consume\":30,\"icon\":\"line02_04\"},{\"id\":9,\"plStepName\":\"MPV车型生产环节5\",\"step\":5,\"power\":100,\"consume\":30,\"icon\":\"line02_05\"},{\"id\":10,\"plStepName\":\"MPV车型生产环节6\",\"step\":6,\"power\":100,\"consume\":30,\"icon\":\"line02_06\"},{\"id\":11,\"plStepName\":\"MPV车型生产环节7\",\"step\":7,\"power\":100,\"consume\":30,\"icon\":\"line02_07\"},{\"id\":12,\"plStepName\":\"MPV车型生产环节8\",\"step\":8,\"power\":100,\"consume\":30,\"icon\":\"line02_08\"},{\"id\":13,\"plStepName\":\"MPV车型生产环节9\",\"step\":9,\"power\":100,\"consume\":30,\"icon\":\"line02_09\"},{\"id\":14,\"plStepName\":\"MPV车型生产环节10\",\"step\":10,\"power\":100,\"consume\":30,\"icon\":\"line02_10\"},{\"id\":15,\"plStepName\":\"MPV车型生产环节11\",\"step\":11,\"power\":100,\"consume\":30,\"icon\":\"line02_11\"},{\"id\":16,\"plStepName\":\"MPV车型生产环节12\",\"step\":12,\"power\":100,\"consume\":30,\"icon\":\"line02_12\"},{\"id\":17,\"plStepName\":\"MPV车型生产环节13\",\"step\":13,\"power\":100,\"consume\":30,\"icon\":\"line02_13\"},{\"id\":18,\"plStepName\":\"MPV车型生产环节14\",\"step\":14,\"power\":100,\"consume\":30,\"icon\":\"line02_14\"},{\"id\":19,\"plStepName\":\"MPV车型生产环节15\",\"step\":15,\"power\":100,\"consume\":30,\"icon\":\"line02_15\"},{\"id\":20,\"plStepName\":\"MPV车型生产环节16\",\"step\":16,\"power\":100,\"consume\":30,\"icon\":\"line02_16\"},{\"id\":21,\"plStepName\":\"MPV车型生产环节17\",\"step\":17,\"power\":100,\"consume\":30,\"icon\":\"line02_17\"},{\"id\":22,\"plStepName\":\"MPV车型生产环节18\",\"step\":18,\"power\":100,\"consume\":30,\"icon\":\"line02_18\"},{\"id\":23,\"plStepName\":\"MPV车型生产环节19\",\"step\":19,\"power\":100,\"consume\":30,\"icon\":\"line02_19\"},{\"id\":24,\"plStepName\":\"MPV车型生产环节20\",\"step\":20,\"power\":100,\"consume\":30,\"icon\":\"line02_20\"},{\"id\":25,\"plStepName\":\"轿车车型生产环节1\",\"step\":1,\"power\":100,\"consume\":30,\"icon\":\"line01_01\"},{\"id\":26,\"plStepName\":\"轿车车型生产环节2\",\"step\":2,\"power\":100,\"consume\":30,\"icon\":\"line01_02\"},{\"id\":27,\"plStepName\":\"轿车车型生产环节3\",\"step\":3,\"power\":100,\"consume\":30,\"icon\":\"line01_03\"},{\"id\":28,\"plStepName\":\"轿车车型生产环节4\",\"step\":4,\"power\":100,\"consume\":30,\"icon\":\"line01_04\"},{\"id\":29,\"plStepName\":\"轿车车型生产环节5\",\"step\":5,\"power\":100,\"consume\":30,\"icon\":\"line01_05\"},{\"id\":30,\"plStepName\":\"轿车车型生产环节6\",\"step\":6,\"power\":100,\"consume\":30,\"icon\":\"line01_06\"},{\"id\":31,\"plStepName\":\"轿车车型生产环节7\",\"step\":7,\"power\":100,\"consume\":30,\"icon\":\"line01_07\"},{\"id\":32,\"plStepName\":\"轿车车型生产环节8\",\"step\":8,\"power\":100,\"consume\":30,\"icon\":\"line01_08\"},{\"id\":33,\"plStepName\":\"轿车车型生产环节9\",\"step\":9,\"power\":100,\"consume\":30,\"icon\":\"line01_09\"},{\"id\":34,\"plStepName\":\"轿车车型生产环节10\",\"step\":10,\"power\":100,\"consume\":30,\"icon\":\"line01_10\"},{\"id\":35,\"plStepName\":\"轿车车型生产环节11\",\"step\":11,\"power\":100,\"consume\":30,\"icon\":\"line01_11\"},{\"id\":36,\"plStepName\":\"轿车车型生产环节12\",\"step\":12,\"power\":100,\"consume\":30,\"icon\":\"line01_12\"},{\"id\":37,\"plStepName\":\"轿车车型生产环节13\",\"step\":13,\"power\":100,\"consume\":30,\"icon\":\"line01_13\"},{\"id\":38,\"plStepName\":\"轿车车型生产环节14\",\"step\":14,\"power\":100,\"consume\":30,\"icon\":\"line01_14\"},{\"id\":39,\"plStepName\":\"轿车车型生产环节15\",\"step\":15,\"power\":100,\"consume\":30,\"icon\":\"line01_15\"},{\"id\":40,\"plStepName\":\"轿车车型生产环节16\",\"step\":16,\"power\":100,\"consume\":30,\"icon\":\"line01_16\"},{\"id\":41,\"plStepName\":\"轿车车型生产环节17\",\"step\":17,\"power\":100,\"consume\":30,\"icon\":\"line01_17\"},{\"id\":42,\"plStepName\":\"轿车车型生产环节18\",\"step\":18,\"power\":100,\"consume\":30,\"icon\":\"line01_18\"},{\"id\":43,\"plStepName\":\"轿车车型生产环节19\",\"step\":19,\"power\":100,\"consume\":30,\"icon\":\"line01_19\"},{\"id\":44,\"plStepName\":\"轿车车型生产环节20\",\"step\":20,\"power\":100,\"consume\":30,\"icon\":\"line01_20\"},{\"id\":45,\"plStepName\":\"SUV车型生产环节1\",\"step\":1,\"power\":100,\"consume\":30,\"icon\":\"line03_01\"},{\"id\":46,\"plStepName\":\"SUV车型生产环节2\",\"step\":2,\"power\":100,\"consume\":30,\"icon\":\"line03_02\"},{\"id\":47,\"plStepName\":\"SUV车型生产环节3\",\"step\":3,\"power\":100,\"consume\":30,\"icon\":\"line03_03\"},{\"id\":48,\"plStepName\":\"SUV车型生产环节4\",\"step\":4,\"power\":100,\"consume\":30,\"icon\":\"line03_04\"},{\"id\":49,\"plStepName\":\"SUV车型生产环节5\",\"step\":5,\"power\":100,\"consume\":30,\"icon\":\"line03_05\"},{\"id\":50,\"plStepName\":\"SUV车型生产环节6\",\"step\":6,\"power\":100,\"consume\":30,\"icon\":\"line03_06\"},{\"id\":51,\"plStepName\":\"SUV车型生产环节7\",\"step\":7,\"power\":100,\"consume\":30,\"icon\":\"line03_07\"},{\"id\":52,\"plStepName\":\"SUV车型生产环节8\",\"step\":8,\"power\":100,\"consume\":30,\"icon\":\"line03_08\"},{\"id\":53,\"plStepName\":\"SUV车型生产环节9\",\"step\":9,\"power\":100,\"consume\":30,\"icon\":\"line03_09\"},{\"id\":54,\"plStepName\":\"SUV车型生产环节10\",\"step\":10,\"power\":100,\"consume\":30,\"icon\":\"line03_10\"},{\"id\":55,\"plStepName\":\"SUV车型生产环节11\",\"step\":11,\"power\":100,\"consume\":30,\"icon\":\"line03_11\"},{\"id\":56,\"plStepName\":\"SUV车型生产环节12\",\"step\":12,\"power\":100,\"consume\":30,\"icon\":\"line03_12\"},{\"id\":57,\"plStepName\":\"SUV车型生产环节13\",\"step\":13,\"power\":100,\"consume\":30,\"icon\":\"line03_13\"},{\"id\":58,\"plStepName\":\"SUV车型生产环节14\",\"step\":14,\"power\":100,\"consume\":30,\"icon\":\"line03_14\"},{\"id\":59,\"plStepName\":\"SUV车型生产环节15\",\"step\":15,\"power\":100,\"consume\":30,\"icon\":\"line03_15\"},{\"id\":60,\"plStepName\":\"SUV车型生产环节16\",\"step\":16,\"power\":100,\"consume\":30,\"icon\":\"line03_16\"},{\"id\":61,\"plStepName\":\"SUV车型生产环节17\",\"step\":17,\"power\":100,\"consume\":30,\"icon\":\"line03_17\"},{\"id\":62,\"plStepName\":\"SUV车型生产环节18\",\"step\":18,\"power\":100,\"consume\":30,\"icon\":\"line03_18\"},{\"id\":63,\"plStepName\":\"SUV车型生产环节19\",\"step\":19,\"power\":100,\"consume\":30,\"icon\":\"line03_19\"},{\"id\":64,\"plStepName\":\"SUV车型生产环节20\",\"step\":20,\"power\":100,\"consume\":30,\"icon\":\"line03_20\"}]}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jian_kong);
        initView();
        getInfo();
    }


    private void initView() {
        ivBack = (Button) findViewById(R.id.iv_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);

        tvTitle.setText("生产监控及修复");
        ivBack.setOnClickListener(this);
        myPopupWindow = new MyPopupWindow(getApplicationContext());

        getChildView(getWindow().getDecorView(), buttons);

    }

    private void getInfo() {
//                data = new Gson().fromJson(response.body().string(), JianKongBean.class).getData();

                data = new Gson().fromJson(json, JianKongBean.class).getData();

                initState();
    }

    private void initState() {
        for (ImageButton button : buttons) {
            int tag = Integer.parseInt(button.getTag().toString());
            if (data.get(tag + 39).getPower() == 100) {
                runOnUiThread(() -> button.setImageResource(R.drawable.ic_rep_warning));
            }
        }
    }

    private void getChildView(View view, List<ImageButton> buttons) {
        ViewGroup viewGroup = (ViewGroup) view;
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View childAt = viewGroup.getChildAt(i);
            if (childAt instanceof ViewGroup) {
                getChildView(childAt, buttons);
            } else if (childAt instanceof ImageButton){
                childAt.setOnClickListener(this);
                buttons.add((ImageButton) childAt);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                return;
        }

        if (v instanceof ImageButton) {
            int tag = Integer.parseInt(v.getTag().toString());
            myPopupWindow.changeInfo(data.get(tag + 39));
            myPopupWindow.showAsDropDown(v, dpTopx(25) - (int)(150*1.75+0.5f)/2, -dpTopx(50) - (int)(130*1.75+0.5f));
        }
    }

    private int dpTopx(int dp) {
        return (int)(dp * 1.75 + 0.5f);
    }
}
